package com.scp.adapters;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.scp.R;
import com.scp.modelo.Registro;
import com.scp.util.Util;

public class RegistrosAdapter extends BaseAdapter {

	private ArrayList<Registro> registros = null;
	private Context context = null;
	
	public RegistrosAdapter(Context context, ArrayList<Registro> registros) {
		super();
		this.context = context;
		this.registros = registros;
	}
	
	@Override
	public int getCount() {
		if (this.registros == null) {
			return 0;			
		} else {
			return this.registros.size();
		}
		
	}

	@Override
	public Registro getItem(int position) {
		return registros.get(position);
	}

	@Override
	public long getItemId(int position) {
		return registros.get(position).get_id();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if(convertView == null){
			LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = inflater.inflate(R.layout.list_registros_item, null);			
		}
		
		TextView textView = (TextView) convertView.findViewById(R.id_list_registros.tvTempo);
		textView.setText(Util.dateToString(getItem(position).getTempo(), "HH:mm"));
		
		ImageView imgTipo = (ImageView) convertView.findViewById(R.id_list_registros.imgTipo);
		if (position % 2 == 0) {
			imgTipo.setImageDrawable(context.getResources().getDrawable(R.drawable.entrada));
		} else {
			imgTipo.setImageDrawable(context.getResources().getDrawable(R.drawable.saida));
		}
		
		return convertView;
	}

}
